package com.yoomiito.app.ui.my;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.my.MyIncome2Adapter;
import com.yoomiito.app.adapter.my.MyIncomeAdapter;
import com.yoomiito.app.adapter.my.MyIncomeDetail2Adapter;
import com.yoomiito.app.adapter.my.MyIncomeDetailAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.IncomeBean;
import com.yoomiito.app.model.my.IncomeInfo;
import com.yoomiito.app.present.my.activity.MyIncomerDetailPresenter;
import com.yoomiito.app.present.my.activity.MyIncomerPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 历史总收入
 */
public class MyIncomeDetailActivity extends BaseActivity<MyIncomerDetailPresenter> {

    private int page = 1;
    private BaseQuickAdapter mAdapter;
    private View empty;
    private String month ;
    private int type;
    @BindView(R.id.rcy)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;



    @Override
    public int getLayoutId() {
        return R.layout.activity_my_income_detail;
    }


    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .titleBarMarginTop(R.id.title_bar).init();
    }

    @Override
    public MyIncomerDetailPresenter newP() {
        return new MyIncomerDetailPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        month = getIntent().getStringExtra("month");
        type = getIntent().getIntExtra("type",0);
        findViewById(R.id.iv_back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView tv = findViewById(R.id.tv_center);
        tv.setText("明细");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        empty = LayoutInflater.from(context).inflate(R.layout.item_live_empty, null, false);
        TextView text = empty.findViewById(R.id.item_empty_tv);
        text.setText("");
        if(type == 6){
            getP().getCreditList(page, month);
        }else{
            getP().getAgentRebate(page, month);
        }

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if(type == 6){
                    getP().getCreditList(page, month);
                }else{
                    getP().getAgentRebate(page, month);
                }
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if(type == 6){
                    getP().getCreditList(page, month);
                }else{
                    getP().getAgentRebate(page, month);
                }
            }
        });
        setAdapter();
    }

    private void setAdapter() {
        if(type == 6){
            mAdapter = new MyIncomeDetailAdapter();
        }else{
            mAdapter = new MyIncomeDetail2Adapter();
        }
        recyclerView.setAdapter(mAdapter);

    }



    public void setData1(List<IncomeInfo> data) {
        if (page == 1) {
            if (data == null || data.size() <= 0) {
                mAdapter.setNewData(null);
                mAdapter.setEmptyView(empty);
            } else {
                mAdapter.setNewData(data);
            }
            smartRefreshLayout.setNoMoreData(false);
        } else {
            smartRefreshLayout.finishLoadMore(true);
            if (data == null || data.size() <= 0) {
                Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show();
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data);
            }
        }
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

}
